﻿<?php

/**
 * @author pigsunny
 * @copyright 2011
 */
 require_once("./common.php");
 require_once("./utils.php");
 require_once("./utilsExtend.php");
 
 class TroopCron{
    function updateTroops($param){
    	//选出达成时间的队列，如果为空则跳过不执行
    	$rows=sql_fetch_rows("select * from sys_troops where endtime >0 and endtime <= unix_timestamp() and (state=0 || state=1)");
    	if(!empty($rows)){
			foreach($rows as $row){
				if($row['task']>1){//先检查出征战术
					$wid=cid2wid($row[targetcid]);
					$t_type=sql_fetch_one("select * from mem_world where wid=".$wid);
					if(!$resistuid) $resistuid=0;
					$resistuid=sql_fetch_one_cell("select uid from sys_city where cid=".$t_type['ownercid']);
					if (!sql_fetch_rows("select * from sys_user_tactics where uid='$row[uid]'")){
						$plunder="1,2,1;2,1,2;3,1,3;4,1,4;5,1,5;6,1,6;7,1,7;8,1,8;9,2,9;10,1,10;11,1,11;12,1,12";
						$invade="1,2,1,2,1;2,1,18,1,2;3,1,3,1,3;4,1,18,1,4;5,1,18,1,5;6,1,15,1,6;7,1,18,1,7;8,1,18,1,8;9,2,9,2,9;10,1,15,1,10;11,1,18,1,11;12,1,15,1,12";
						$field="1,2,1;2,1,2;3,1,3;4,1,4;5,1,5;6,1,6;7,1,7;8,1,8;9,2,9;10,1,10;11,1,11;12,1,12";
						$patrol="3,1,3";
						sql_query("insert into sys_user_tactics(uid,plunder,invade,field,patrol) values ('$row[uid]','$plunder','$invade','$field','$patrol')");
					}
					if (!sql_fetch_rows("select * from sys_user_tactics where uid='$resistuid'")){
						$plunder="1,2,1;2,1,2;3,1,3;4,1,4;5,1,5;6,1,6;7,1,7;8,1,8;9,2,9;10,1,10;11,1,11;12,1,12";
						$invade="1,2,1,2,1;2,1,18,1,2;3,1,3,1,3;4,1,18,1,4;5,1,18,1,5;6,1,15,1,6;7,1,18,1,7;8,1,18,1,8;9,2,9,2,9;10,1,15,1,10;11,1,18,1,11;12,1,15,1,12";
						$field="1,2,1;2,1,2;3,1,3;4,1,4;5,1,5;6,1,6;7,1,7;8,1,8;9,2,9;10,1,10;11,1,11;12,1,12";
						$patrol="3,1,3";
						sql_query("insert into sys_user_tactics(uid,plunder,invade,field,patrol) values ('$resistuid','$plunder','$invade','$field','$patrol')");
					}
					//加入这个部队的出征战术，但我觉得好像没必要
					//sql_query("insert into sys_user_tactics(uid,plunder,invade,field,patrol) values ('$plunder','$invade','$field','$patrol')");
				}
				$soldiers=$row['soldiers'];
				$soldierArray = explode(",",$soldiers);
				$numSoldiers = array_shift($soldierArray);
				for ($i = 0; $i < $numSoldiers; $i++){
					$sid[$i] = array_shift($soldierArray);
					$cnt[$i] = array_shift($soldierArray);
					if ($sid[$i]==3){$attNum=$cnt[$i];$tempi=$i;}//取出斥候数量
					if ($cnt[$i] < 0) $cnt[$i] = 0;
					$acount+=$cnt[$i];
					$soldierInfo=sql_fetch_one_cell("select hp from cfg_soldier where sid=$sid[$i]");
					$ourhp+=$soldierInfo*$cnt[$i];
				}
				if ($row['state']==1){//军队返回的处理
					//兵回城
					$content=sprintf($GLOBALS['report']['backtitle'],getCityNamePosition($row['targetcid']),$GLOBALS['report']['type'][$row['task']],getCityNamePosition($row['cid']));
					$content.=sprintf($GLOBALS['report']['table_start'],部队返回,军队,数量);
					for ($i = 0; $i < $numSoldiers; $i++){
						addCitySoldier($row['cid'],$sid[$i],$cnt[$i]);
						$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['battle']['patrol_report_soldier'][$sid[$i]]);
						$content.=sprintf($GLOBALS['report']['td'],$cnt[$i]);
						$content.=$GLOBALS['report']['tr_end'];
					}
					$content.=$GLOBALS['report']['table_end'];
					//将回城
					changeHeroProperty($row['cid'],$row['hid'],"0");
					//资源到城里
					$resourceArray=explode(",",$row['resource']);
					addCityResources($row['cid'],$resourceArray[2],$resourceArray[3],$resourceArray[4],$resourceArray[1],$resourceArray[0]);
					$content.=sprintf($GLOBALS['report']['backend'],getCityNamePosition($row['cid']));
					$title=getReportTitle($row['task'],$row['state']);
					sendReport($row['uid'],0,$title,$row['cid'],$row['targetcid'],$content);
					//删除这只队伍
					sql_query("delete from sys_troops where id=$row[id]");
				}
				if ($row['state']==0){//军队出发的处理
					switch ($row['task']){
						case 0:{//运输军队
							//资源送到目的地
							//目的地增加资源
							$myUserInfo=sql_fetch_one("select * from sys_user where uid='$row[uid]'");
							$targetCityInfo=sql_fetch_one("select * from sys_city where cid='$row[targetcid]'");
							$targetUserInfo=sql_fetch_one("select * from sys_user where uid='$targetCityInfo[uid]'");
							$targetunion = $targetUserInfo['union_id'];
							$myunion = $myUserInfo['union_id'];
							$resourceArray=explode(",",$row['resource']);
							if($targetunion==$myunion){
								addCityResources($row['targetcid'],$resourceArray[2],$resourceArray[3],$resourceArray[4],$resourceArray[1],$resourceArray[0]);
								//军队返回
								sql_query("update sys_troops set state=1,starttime=unix_timestamp(),endtime=unix_timestamp()+$row[pathtime],resource='0,0,0,0,0' where id=$row[id]");
								$content=sprintf($GLOBALS['report']['success'],getCityNamePosition($row['targetcid']),$GLOBALS['report']['type'][$row['task']],$GLOBALS['report']['type'][$row['task']]);
								$content.=sprintf($GLOBALS['report']['table_start'],运输到达,兵种,数量);
								for ($i = 0; $i < $numSoldiers; $i++){
									$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['battle']['patrol_report_soldier'][$sid[$i]]);
									$content.=sprintf($GLOBALS['report']['td'],$cnt[$i]);
									$content.=$GLOBALS['report']['tr_end'];
								}
							}else{//军队返回
								sql_query("update sys_troops set state=1,starttime=unix_timestamp(),endtime=unix_timestamp()+$row[pathtime] where id=$row[id]");
								$content=sprintf($GLOBALS['report']['success'],getCityNamePosition($row['targetcid']),$GLOBALS['report']['type'][$row['task']],$GLOBALS['report']['type'][$row['task']]);
								$content.="目标不是自己或盟友城池，运输返回！";
							}
							$content.=$GLOBALS['report']['table_end'];
							$content.=sprintf($GLOBALS['report']['resour_table_start'],携带资源);
							$content.=$GLOBALS['report']['tr_end'];
							$content.=sprintf($GLOBALS['report']['resour_td'],$resourceArray[0],$resourceArray[1],$resourceArray[2],$resourceArray[3],$resourceArray[4]);
							$content.=$GLOBALS['report']['tr_end'];
							$content.=$GLOBALS['report']['table_end'];
							$content.=sprintf($GLOBALS['report']['end'],getCityNamePosition($row['cid']));
							sendReport($row['uid'],'0','1',$row['cid'],$row['targetcid'],$content);
						break;}
						case 1:{//派遣到达
							$myUserInfo=sql_fetch_one("select * from sys_user where uid='$row[uid]'");
							$targetCityInfo=sql_fetch_one("select * from sys_city where cid='$row[targetcid]'");
							$targetUserInfo=sql_fetch_one("select * from sys_user where uid='$targetCityInfo[uid]'");
							$targetunion = $targetUserInfo['union_id'];
							$myunion = $myUserInfo['union_id'];
							//派遣的目标是自己的城池
							if (sql_check("select * from sys_city where cid=$row[targetcid] and uid=$row[uid]")){
								//军队有英雄的话先判断招贤馆还能不能放得下人,将领状态改变
								if($row[hid]>0 && cityHasHeroPosition($row[uid],$row[targetcid])){
									for ($i = 0; $i < $numSoldiers; $i++){
										addCitySoldier($row[targetcid],$sid[$i],$cnt[$i]);
									}
									//资源到城
									$resourceArray=explode(",",$row[resource]);
									addCityResources($row[targetcid],$resourceArray[2],$resourceArray[3],$resourceArray[4],$resourceArray[1],$resourceArray[0]);
									changeHeroProperty($row[cid],$row[hid],"0",true,$row[targetcid]);
									updateCityHeroChange($row[uid],$row[cid]);
									updateCityHeroChange($row[uid],$row[targetcid]);
									//生成报告
									$content=sprintf($GLOBALS['report']['success'],getCityNamePosition($row[targetcid]),$GLOBALS['report']['type'][$row[task]],$GLOBALS['report']['type'][$row[task]]);
									$content.=sprintf($GLOBALS['report']['table_start'],派遣到达,兵种,数量);
									for ($i = 0; $i < $numSoldiers; $i++){
										$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['battle']['patrol_report_soldier'][$sid[$i]]);
										$content.=sprintf($GLOBALS['report']['td'],$cnt[$i]);
										$content.=$GLOBALS['report']['tr_end'];
									}
									$content.=$GLOBALS['report']['table_end'];
									$content.=sprintf($GLOBALS['report']['resour_table_start'],携带资源);
									$content.=$GLOBALS['report']['tr_end'];
									$content.=sprintf($GLOBALS['report']['resour_td'],$resourceArray[0],$resourceArray[1],$resourceArray[2],$resourceArray[3],$resourceArray[4]);
									$content.=$GLOBALS['report']['tr_end'];
									$content.=$GLOBALS['report']['table_end'];
									$content.=sprintf($GLOBALS['report']['backend'],getCityNamePosition($row[targetcid]));
									$title=getReportTitle($row[task],$row[state]);
									sendReport($row[uid],0,$title,$row[cid],$row[targetcid],$content);
									sql_query("delete from sys_troops where id=$row[id]");
									refreshFoodArmyUse($row[targetcid]);
								}else
								if($row[hid]<=0){//军队无英雄
									//资源到城
									$resourceArray=explode(",",$row[resource]);
									addCityResources($row[targetcid],$resourceArray[2],$resourceArray[3],$resourceArray[4],$resourceArray[1],$resourceArray[0]);
									//生成报告
									$content.=sprintf($GLOBALS['report']['success'],getCityNamePosition($row[targetcid]),$GLOBALS['report']['type'][$row[task]],$GLOBALS['report']['type'][$row[task]]);
									$content.=sprintf($GLOBALS['report']['table_start'],派遣到达,军队,数量);
									for ($i = 0; $i < $numSoldiers; $i++){
										//军队入城，生成士兵报告
										addCitySoldier($row[targetcid],$sid[$i],$cnt[$i]);
										$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['battle']['patrol_report_soldier'][$sid[$i]]);
										$content.=sprintf($GLOBALS['report']['td'],$cnt[$i]);
										$content.=$GLOBALS['report']['tr_end'];
									}
									$content.=$GLOBALS['report']['table_end'];
									$content.=sprintf($GLOBALS['report']['resour_table_start'],携带资源);
									$content.=$GLOBALS['report']['tr_end'];
									$content.=sprintf($GLOBALS['report']['resour_td'],$resourceArray[0],$resourceArray[1],$resourceArray[2],$resourceArray[3],$resourceArray[4]);
									$content.=$GLOBALS['report']['tr_end'];
									$content.=$GLOBALS['report']['table_end'];
									$content.=sprintf($GLOBALS['report']['backend'],getCityNamePosition($row[targetcid]));
									$title=getReportTitle($row[task],$row[state]);
									sendReport($row[uid],0,$title,$row[cid],$row[targetcid],$content);
									sql_query("delete from sys_troops where id=$row[id]");
									refreshFoodArmyUse($row[targetcid]);
								}
							}//如果是同盟的地盘则不论是否为城池执行结果都一样
							else{
								if (!empty($targetunion)){//是同盟的地盘
									if (($myunion == $targetunion) && $myunion>0){
										$allow=sql_fetch_one_cell("select allow from sys_allow_union_troop where uid='$targetCityInfo[uid]'");
										if($allow==1) $targetIsUnion = true;
									}
								}
								//是自己野地
								$myfield=sql_fetch_one("select ownercid from mem_world where wid=".cid2wid($row['targetcid']));
								if($row['cid']==$myfield['ownercid']) $ismyfield=true;
								if($targetIsUnion || $ismyfield){
									sql_query("update sys_troops set state=4 where id=$row[id]");
									changeHeroProperty($row['cid'],$row['hid'],"4");
									//生成报告
									$content.=sprintf($GLOBALS['report']['success'],getCityNamePosition($row['targetcid']),$GLOBALS['report']['type'][$row['task']],$GLOBALS['report']['type'][$row['task']]);
									$content.=sprintf($GLOBALS['report']['table_start'],派遣到达,军队,数量);
									for ($i = 0; $i < $numSoldiers; $i++){
										$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['battle']['patrol_report_soldier'][$sid[$i]]);
										$content.=sprintf($GLOBALS['report']['td'],$cnt[$i]);
										$content.=$GLOBALS['report']['tr_end'];
									}
									$content.=$GLOBALS['report']['table_end'];
									$content.=sprintf($GLOBALS['report']['resour_table_start'],携带资源);
									$content.=$GLOBALS['report']['tr_end'];
									$resourceArray=explode(",",$row['resource']);
									$content.=sprintf($GLOBALS['report']['resour_td'],$resourceArray[0],$resourceArray[1],$resourceArray[2],$resourceArray[3],$resourceArray[4]);
									$content.=$GLOBALS['report']['tr_end'];
									$content.=$GLOBALS['report']['table_end'];
									$content.=sprintf($GLOBALS['report']['backend'],getCityNamePosition($row['targetcid']));
								    $title=getReportTitle($row['task'],$row['state']);
								    sendReport($row['uid'],'0',$title,$row['cid'],$row['targetcid'],$content);
								}else{
									sql_query("update sys_troops set state=1,starttime=unix_timestamp(),endtime=unix_timestamp()+$row[pathtime] where id=$row[id]");
									if(!$targetIsUnion) $content.="盟友未允许部队驻军，部队返回！</br>";
									else if(!cityHasHeroPosition($row['uid'],$row['targetcid'])) $content.="目标城池无法容纳更多的将领，部队返回！</br>";
									else $content.="非法目标，部队返回！</br>";
									$content.=sprintf($GLOBALS['report']['end'],getCityNamePosition($row['cid']));
									$title=getReportTitle($row['task'],$row['state']);
									sendReport($row['uid'],'0',$title,$row['cid'],$row['targetcid'],$content);
								}
							}
					    break;}
						case 2:{//侦察
							//$depatrol=sql_fetch_one_cell("select depatrol_join from sys_city_tactics where cid=$row[targetcid]");
							$depatrol=sql_fetch_one_cell("select count from sys_city_soldier where cid=$row[targetcid] and sid=3");
							$te_level=sql_fetch_one_cell("select level from sys_city_technic where tid=7 and cid=$row[cid]");//取出出发城池科技等级
							if(!$depatrol) $depatrol=0;
							$content=sprintf($GLOBALS['report']['success'],getCityNamePosition($row['targetcid']),$GLOBALS['report']['type'][$row['task']],$GLOBALS['report']['type'][$row['task']]);
							if($depatrol*2==$attNum){//平局,获取部分情报
								$cnt[$tempi]=$cnt[$tempi]/2;//攻击方损失
								sql_query("update sys_city_soldier set count=count/2 where sid=3 and cid=$row[targetcid]");//守方军队损失
								$te_level=floor($te_level/2);
								$targetuid=sql_fetch_one_cell("select uid from sys_city where cid=$row[targetcid]");
								if($targetuid>1000){
									$content1="</br>一支部队对我方进行侦察，双方战斗平局！对方获得部分情报！";
									sendReport($targetuid,'0','12',$row['cid'],$row['targetcid'],$content1);
								}
								$content="</br>对方斥候迎战，双方战斗平局！我方获得部分情况！";
							}else if($depatrol*2>$attNum){//守方胜,无任何情报
								$cnt[$tempi]=0;//攻击方全军覆没
								sql_query("update sys_city_soldier set count=count-$attNum where sid=3 and cid=$row[targetcid]");//守方军队损失
								$te_level=0;
								$targetuid=sql_fetch_one_cell("select uid from sys_city where cid=$row[targetcid]");
								if($targetuid>1000){
									$content1="</br>一支部队对我方进行侦察，我方战斗胜利！对方没有获得任何情报！";
									sendReport($targetuid,'0','12',$row['cid'],$row['targetcid'],$content1);
								}
								$content="</br>对方斥候迎战，";
							}else{//攻方胜
								$cnt[$tempi]=$cnt[$tempi]-$depatrol*2;//攻击方全军覆没,获取全部情报
								sql_query("update sys_city_soldier set count=0 where sid=3 and cid=$row[targetcid]");//守方军队损失
							}
							$soldiers="";
							for($i=0;$i<$numSoldiers;$i++){//重新生成军队
								$soldiers.=$sid[$i].",".$cnt[$i].",";
							}
							$soldiers=$numSoldiers.",".$soldiers;
							if($numSoldiers==1 && $cnt[$tempi]==0){//攻方只有斥候且死光
								sql_query("delete from sys_troops where id=$row[id]");
							}else sql_query("update sys_troops set state=1,starttime=unix_timestamp(),endtime=unix_timestamp()+$row[pathtime],soldiers='$soldiers' where id=$row[id]");
							$blood=floor(100-100*$cnt[$tempi]/$attNum);//将领失血
							if($row['hid']>0) sql_query("UPDATE mem_hero_blood SET `force` =`force`-$blood where hid=".$row['hid']);
							if($te_level==0) $content.="我军全军覆没，没有获得任何情报!";
							//是否为城池
							if(sql_check("select cid from sys_city where cid=$row[targetcid]")){
								$resources=sql_fetch_one("select * from mem_city_resource where cid=$row[targetcid]");
								$resource_buildings=sql_fetch_rows("select * from sys_building where cid=$row[targetcid] order by bid,level desc");
								$bids=sql_fetch_rows("select bid from sys_building where cid=$row[targetcid] group by bid");
								$resourceArray=array(0=>floor($resources['gold']),1=>floor($resources['food']),2=>floor($resources['wood']),3=>floor($resources['rock']),4=>floor($resources['iron']),5=>floor($resources['people']),6=>floor($resources['morale']));
								if($te_level>=1){
									$content.=sprintf($GLOBALS['report']['resour_table_start'],城外资源);
									$content.=$GLOBALS['report']['tr_end'];
									$content.=sprintf($GLOBALS['report']['resour_td'],$resourceArray[0],$resourceArray[1],$resourceArray[2],$resourceArray[3],$resourceArray[4]);
								}
								if($te_level>=2){//2级
									$content.=$GLOBALS['report']['table_end'];
									$content.=sprintf($GLOBALS['report']['table_start'],城内信息,建筑,等级);
									if(!empty($bids)){
									foreach($bids as $bid){//3级4级
										if($bid['bid']<5){
											foreach($resource_buildings as $resource_building){
												if($resource_building['bid']==$bid['bid']){
													$building_level.=$resource_building['level'].",";
												}
											}
											$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['Resource']['building'][$bid['bid']]);
											$content.=sprintf($GLOBALS['report']['td'],$building_level);
											$content.=$GLOBALS['report']['tr_end'];
											$building_level="";
										}elseif($te_level>=3 && $bid['bid']<7){
											$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['Resource']['building'][$bid['bid']+16]);
											$content.=sprintf($GLOBALS['report']['td'],$resourceArray[$bid['bid']]);
											$content.=$GLOBALS['report']['tr_end'];
										}elseif($te_level>=4){
											foreach($resource_buildings as $resource_building){
												if($resource_building['bid']==$bid['bid']){
													$building_level.=$resource_building['level'].",";
												}
											}
											$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['Resource']['building'][$bid['bid']]);
											$content.=sprintf($GLOBALS['report']['td'],$building_level);
											$content.=$GLOBALS['report']['tr_end'];
											$building_level="";
										}
									}
									}
								}
								if($te_level>=5){//5级
									$defences=sql_fetch_rows("select * from sys_city_defence where cid=$row[targetcid]");
									if(!empty($defences)){
										$content.=$GLOBALS['report']['table_end'];
										$content.=sprintf($GLOBALS['report']['table_start'],城防信息,种类,数量);
										foreach($defences as $defence){
											$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['Resource']['defences'][$defence['did']]);
											$content.=sprintf($GLOBALS['report']['td'],$defence['count']);
											$content.=$GLOBALS['report']['tr_end'];
										}
									}
								}
								if($te_level>=6){//6级
									createmapSoldier($row[targetcid]);
									$solds=sql_fetch_rows("select * from sys_city_soldier where cid=$row[targetcid]");
									if(!empty($solds)){
										$content.=$GLOBALS['report']['table_end'];
										$content.=sprintf($GLOBALS['report']['table_start'],城内军队,兵种,数量);
										foreach($solds as $sold){
											if($sold['count']>0){
												$count=$sold['count'];
												if($count<10) $count_desc="几个";
												elseif(10<=$count && $count<25) $count_desc="小队";
												elseif(25<=$count && $count<100) $count_desc="一些";
												elseif(100<=$count && $count<250) $count_desc="一群";
												elseif(250<=$count && $count<1000) $count_desc="许多";
												elseif(1000<=$count && $count<2500) $count_desc="大队";
												elseif(2500<=$count && $count<5000) $count_desc="大群";
												elseif(5000<=$count && $count<10000) $count_desc="巨量";
												elseif(10000<=$count) $count_desc="无数";
												$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['battle']['patrol_report_soldier'][$sold['sid']]);
												if($te_level>=7) $content.=sprintf($GLOBALS['report']['td'],$count);
												else $content.=sprintf($GLOBALS['report']['td'],$count_desc);
												$content.=$GLOBALS['report']['tr_end'];
											}
										}
									}
								}
								if($te_level>=7){//7级
									$mengyous=sql_fetch_rows("select * from sys_troops where state=4 and targetcid=$row[targetcid]");
									if(!empty($mengyous)){//有盟友的军队驻扎
										$content.=$GLOBALS['report']['table_end'];
										$content.=sprintf($GLOBALS['report']['table_start'],盟友军队,兵种,数量);
										foreach($mengyous as $mengyou){
											$sold=$mengyou['soldiers'];
											$soldArray = explode(",",$sold);
											$num = array_shift($soldArray);
											for ($i = 0; $i < $num; $i++){
												$msid[$i] = array_shift($soldArray);
												$mcnt[$i] = array_shift($soldArray);
												if ($mcnt[$i] < 0) $mcnt[$i] = 0;
												$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['battle']['patrol_report_soldier'][$msid[$i]]);
												$content.=sprintf($GLOBALS['report']['td'],$sold['count']);
												$content.=$GLOBALS['report']['tr_end'];
											}
										}
									}
								}
								if($te_level>=8){//8级
									$has_heros=sql_fetch_rows("select * from sys_city_hero where cid=$row[targetcid]");
									if(!empty($has_heros)){
										$content.=$GLOBALS['report']['table_end'];
										$content.=sprintf($GLOBALS['report']['table_start'],城内将领,将领,等级);
										foreach($has_heros as $has_hero){
											$content.=sprintf($GLOBALS['report']['td'],$has_hero['name']);
											$content.=sprintf($GLOBALS['report']['td'],$has_hero['level']);
											$content.=$GLOBALS['report']['tr_end'];
										}
									}
								}
								if($te_level>=9){//9级
									$technics=sql_fetch_rows("select * from sys_city_technic where cid=$row[targetcid]");
									if(!empty($technics)){
										$content.=$GLOBALS['report']['table_end'];
										$content.=sprintf($GLOBALS['report']['table_start'],城内科技,科技,等级);
										foreach($technics as $technic){
											$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['Resource']['technics'][$technic['tid']]);
											$content.=sprintf($GLOBALS['report']['td'],$technic['level']);
											$content.=$GLOBALS['report']['tr_end'];
										}
									}
								}
								if($te_level>=10){//10级
									$targetCityInfo=sql_fetch_one_cell("select uid from sys_city where cid=$row[targetcid]");
									if($targetCityInfo>1000){
									$last_time=sql_fetch_one_cell("select lastupdate from sys_online where uid=$targetCityInfo");
									$now_time=sql_fetch_one_cell("select unix_timestamp()");
									if($now_time-$last_time<=10) $last_login="在线";
									else{
										$last_login=sql_fetch_one_cell("select from_unixtime($last_time)");
										$last_login="离线。下线时间：".$last_login;
									}
									$content.=$GLOBALS['report']['table_end'];
									$content.="此玩家目前状态为：".$last_login;
									//file_put_contents("log.txt",$last_login."\r\n",FILE_APPEND);
									}
								}
							}else{//是野地
							//	file_put_contents("log.txt",$block."2\r\n",FILE_APPEND);
								createmapSoldier($row[targetcid]);
								$content.=sprintf($GLOBALS['report']['table_start'],侦察结果,兵种,数量);
								if($te_level>=1){
									$solds=sql_fetch_rows("select * from sys_city_soldier where cid=$row[targetcid]");
									foreach($solds as $sold){
										if($sold['count']>0){
											$count=$sold['count'];
											if($te_level<1){
												if($count<10) $count_desc="几个";
												elseif(10<=$count && $count<25) $count_desc="小队";
												elseif(25<=$count && $count<100) $count_desc="一些";
												elseif(100<=$count && $count<250) $count_desc="一群";
												elseif(250<=$count && $count<1000) $count_desc="许多";
												elseif(1000<=$count && $count<2500) $count_desc="大队";
												elseif(2500<=$count && $count<5000) $count_desc="大群";
												elseif(5000<=$count && $count<10000) $count_desc="巨量";
												elseif(10000<=$count) $count_desc="无数";
											}
											$content.=sprintf($GLOBALS['report']['td'],$GLOBALS['battle']['patrol_report_soldier'][$sold['sid']]);
											if($te_level>2) $content.=sprintf($GLOBALS['report']['td'],$count);
											else $content.=sprintf($GLOBALS['report']['td'],$count_desc);
											$content.=$GLOBALS['report']['tr_end'];
										}
									}
								}
								if($te_level>=5){
									$has_heros=sql_fetch_rows("select * from sys_city_hero where cid=$row[targetcid]");
									if(!empty($has_heros)){
										$content.=$GLOBALS['report']['table_end'];
										$content.=sprintf($GLOBALS['report']['table_start'],野地将领,将领,等级);
										foreach($has_heros as $has_hero){
											$content.=sprintf($GLOBALS['report']['td'],$has_hero['name']);
											$content.=sprintf($GLOBALS['report']['td'],$has_hero['level']);
											$content.=$GLOBALS['report']['tr_end'];
										}
									}
								}
								if($te_level>=5){
								}
							}
							$content.=$GLOBALS['report']['table_end'];
							$content.=sprintf($GLOBALS['report']['end'],getCityNamePosition($row['cid']));
							sendReport($row['uid'],'0','5',$row['cid'],$row['targetcid'],$content);
							$nowtime=sql_fetch_one_cell("select unix_timestamp()");
							$resistuid=sql_fetch_one_cell("select uid from sys_city where cid=".$t_type['ownercid']);
							if(!$resistuid) $resistuid=0;
								if($t_type['type']>0 and $resistuid<1000) $run=($ourhp<$npcvalues)?1:0;
							sql_query("replace into sys_battle(id,type,starttime,endtime,cid,attackuid,resistuid,result) values('$row[id]','0','$nowtime','$nowtime','$row[targetcid]','$row[uid]','$resistuid','4')");
						break;}
						case 3 or 4:{//掠夺
							//进入战斗界面,如果目标已经有军队在战斗则等待
							if(sql_check("select * from sys_troops where state=3 and targetcid=".$row['targetcid'])){
								sql_query("update sys_troops set state=2 where id=".$row['id']);
							}else{
								$run=1;
								$ourhp/=3;
								$wid=cid2wid($row['targetcid']);
								$t_type=sql_fetch_one("select * from mem_world where wid=".$wid);
								$resistuid=sql_fetch_one_cell("select uid from sys_city where cid=".$row['targetcid']);
								$npcvalues=sql_fetch_one_cell("select npcvalue from cfg_field_npcvalue where level=$t_type[level]");
								if(!$resistuid) $resistuid=0;
								if($t_type['type']>0 and $resistuid<1000) $run=($ourhp<$npcvalues)?1:0;
								if($run){//没有吓跑
									$nowtime=sql_fetch_one_cell("select unix_timestamp()");
									sql_query("update sys_troops set state=3,battleid=id,battlefieldid=targetcid,startcid=cid where id=".$row['id']);
									sql_query("replace into sys_battle(id,type,starttime,cid,attackuid,resistuid,result) values('$row[id]','2','$nowtime','$row[targetcid]','$row[uid]','$resistuid','3')");
									//取得敌方英雄
									if($t_type['type']==0){
										$resisthid=sql_fetch_one_cell("select hid from sys_city_hero where state=7 and cid=".$row['targetcid']);
										if(empty($resisthid)) $resisthid=sql_fetch_one_cell("select hid from sys_city_hero where state=1 and cid=".$row['targetcid']);
									}else{
										$resisthid=sql_fetch_one_cell("select hid from sys_city_hero where cid=".$row['targetcid']);
									}
									if(empty($resisthid)){
										$resisthid=0;
										$state=0;
									}else $state=sql_fetch_one_cell("select state from sys_city_hero where hid=$resisthid");
									//防守方取得加成
									//file_put_contents("log.txt",$resisthid."<-\r\n",FILE_APPEND);
									$def_add=getAdd($row[targetcid],$resisthid);
									$def_command=array_shift($def_add);
									$def_attc=array_shift($def_add);
									$def_def=array_shift($def_add);
									$def_speed=array_shift($def_add);
									$def_blood=array_shift($def_add);
									$def_plund=array_shift($def_add);
									$def_command*=100/$acount;
									if($def_command>1) $def_command=1;
									//敌方应用
									//取出防守部队
									if($resistuid<1000) createmapSoldier($row['targetcid']);
									if($resistuid>1000 && $t_type['type']==0){
										//取出出城的士兵
										$dtactics=sql_fetch_one("select * from sys_city_tactics where cid=$row[targetcid]");
										if(!$dtactics){
											$deplunder_join="2,4,5,6,7,8,10,12";
											$deplunder="1,2,1;2,1,2;3,2,3;4,1,4;5,1,5;6,1,6;7,1,7;8,1,8;9,2,9;10,1,10;11,1,11;12,1,12";
											$depatrol_join="3";
											$depatrol="3,1,3";
											$deinvade_join="2,4,5,6,7,8,10,11,12";
											$deinvade="1,2,1,2,1;2,1,2,1,2;3,2,3,2,3;4,1,4,1,4;5,1,5,1,5;6,2,6,1,6;7,1,7,1,7;8,1,8,1,8;9,2,9,2,9;10,2,10,1,10;11,2,11,1,11;12,2,12,1,12;15,2,6;16,2,4;17,2,11";
											sql_query("insert into sys_city_tactics(cid,deplunder_join,deplunder,depatrol_join,depatrol,deinvade_join,deinvade) values ('$row[targetcid]','$deplunder_join','$deplunder','$depatrol_join','$depatrol','$deinvade_join','deinvade')");
											$dtactics=sql_fetch_one("select * from sys_city_tactics where cid=$row[targetcid]");
										}
										$do_sid=($row['task']==3)?$dtactics['deplunder_join']:$dtactics['deinvade_join'];
										$dtactic=($row['task']==3)?$dtactics['deplunder']:$dtactics['deinvade'];
										$temp=($row['task']==3)?"deplunder":"deinvade";
										$dsoldiers=sql_fetch_rows("select * from sys_city_soldier where sid in ($do_sid) and cid=$row[targetcid]");
									}else{
										$dsoldiers=sql_fetch_rows("select * from sys_city_soldier where cid=".$row['targetcid']);
										$dtactics=sql_fetch_one("select * from sys_user_tactics where uid=".$resistuid);
										if($row['task']==4 and $t_type['type']==0){
											$dtactic="1,2,1,2,1;2,1,2,1,2;3,1,3,2,3;4,1,4,1,4;5,1,5,1,5;6,1,6,1,6;7,1,7,1,7;8,1,8,1,8;9,2,9,2,9;10,1,10,1,10;11,1,11,1,11;12,1,12,1,12;15,2,6;16,2,4;17,2,11";
											$temp="ye!";
										}
										else{
											$dtactic=($t_type['type']>0)?$dtactics['field']:$dtactics['plunder'];
											$temp=($t_type['type']>0)?"field":"plunder";
										}
									}
									//file_put_contents("log.txt",$temp."<-\r\n",FILE_APPEND);
									//兵加成
									if(!empty($dsoldiers)){
										$d_numsoldire=count($dsoldiers);
										$i=0;
										foreach($dsoldiers as $dsoldier){
											$d_sid[$i]=$dsoldier['sid'];
											$d_cnt[$i]=$dsoldier['count'];
											$soldierinfo=sql_fetch_one("select * from cfg_soldier where sid=".$dsoldier['sid']);
											$d_type[$i]=$soldierinfo['type'];
											$resistsoldiers.=$dsoldier['sid'].",".$dsoldier['count'].",";
											//应用加成
											$shoot[$i]=getshoot($d_sid[$i],$row['targetcid']);
											$att[$i]=floor($soldierinfo['ap']*(1+$def_command*$def_attc/100));
											$def[$i]=floor($soldierinfo['dp']*(1+$def_command*$def_def/100));
											$blood[$i]=floor($soldierinfo['hp']*(1+$def_command*$def_blood/100));
											$speed[$i]=floor($def_speed+$soldierinfo['speed']);
											$i++;
										}
										//$resistpos=$d_numsoldire.",".$resistpos;
										$resistsoldiers=$d_numsoldire.",".$resistsoldiers;
									}else {$resistsoldiers="1,1,1";$resistpos="1,1,100";}
									//敌方加成
									//城防
									$resistdefence=0;
									$walllevel=0;
									$wallhp=0;
									if($row['task']==4 and $t_type['type']==0){
										//if($resistuid<1000) createdefenceSoldier($row['targetcid']);
										$walllevel=sql_fetch_one_cell("select level from sys_building where cid='$row[targetcid]' and bid=".ID_BUILDING_WALL);
										$wallhp=$walllevel*1000000;
										$defences=sql_fetch_rows("select * from sys_city_defence where cid=".$row['targetcid']);
										//$did=sql_fetch_rows("select did from sys_city_defence where cid=".$row['targetcid']);
										//$defence = implode(",",$temp);
										if(!empty($defences) && $walllevel>0){
											sql_query("replace into mem_battle_round (battleid,attack,type,sid,count,speed,command,attc,def,hp,pos,shoot) values ('$row[id]','2','18','6','$wallhp','0','$def_command','0','0','1','100','0')");
											$dcount=count($defences);
											$i=0;
											foreach($defences as $defence){
												$d_did[$i]=$defence['did'];
												$df_cnt[$i]=$defence['count'];
												$resistdefence.=$d_did[$i].",,".$df_cnt[$i].",";
												$didtype[$i]=$d_did[$i]+12;
												$defenceinfo=sql_fetch_one("select * from cfg_defence where did=$d_did[$i]");
												sql_query("replace into mem_battle_round (battleid,attack,type,sid,count,speed,command,attc,def,hp,pos,shoot) values ('$row[id]','2','$didtype[$i]','$d_did[$i]','$df_cnt[$i]','0','$def_command','$defenceinfo[ap]','$defenceinfo[dp]','$defenceinfo[hp]','100','$defenceinfo[range]')");
												$i++;
											}
											$resistdefence=$dcount.",".$resistdefence;
										}
									}
									//战术
									//file_put_contents("log.txt",$dtactic."\r\n",FILE_APPEND);
									$atacticArray = explode(";",$dtactic);
									$numatactics = count($atacticArray);
									for ($i=0;$i<$numatactics;$i++){
										$soldirs=explode(",",$atacticArray[$i]);
										$atype = array_shift($soldirs);
										if($atype<13){//是士兵
											for($j=0;$j<$d_numsoldire;$j++){
												if($atype==$d_sid[$j]){
													$action = array_shift($soldirs);
													$target = array_shift($soldirs);
													if($row['task']==4 and $t_type['type']==0 and $atype<15){
														$action2 = array_shift($soldirs);
														$target2 = array_shift($soldirs);
														$pos=0;
														$d_task=6;
													}else{
														$action2 = $action;
														$target2 = $target;
														$d_task=5;
														switch ($action){
															case 1:{$pos=100;break;}
															case 2:{$pos=50;break;}
															case 3:{$pos=0;break;}
														}
													}
													$resistpos.=$atype.",".$pos.",";
													sql_query("replace into		mem_battle_tactics(battleid,attack,stype,action,target,action2,target2) values('$row[id]','0','$atype','$action','$target','$action2','$target2')");
													sql_query("replace into mem_battle_round (battleid,attack,type,sid,count,speed,command,attc,def,hp,pos,shoot) values ('$row[id]','0','$d_type[$j]','$d_sid[$j]','$d_cnt[$j]','$speed[$j]','$def_command','$att[$j]','$def[$j]','$blood[$j]','$pos','$shoot[$j]')");
												}
											}
										}else{//是城防
										if($walllevel>0){
											for($j=0;$j<$dcount;$j++){
												if($atype==($didtype[$j])){
													$action = array_shift($soldirs);
													$target = array_shift($soldirs);
													$action2 = $action;
													$target2 = $target;
													sql_query("replace into		mem_battle_tactics(battleid,attack,stype,action,target,action2,target2) values('$row[id]','2','$atype','$action','$target','$action2','$target2')");
												}
											}
										}
										}
									}
									$resistpos=$d_numsoldire.",".$resistpos;
									//file_put_contents("log.txt",$resistpos."\r\n",FILE_APPEND);
									//取得战场距离
									$fieldrange=getBattleRange($row['cid'],$row['soldiers'],$row['targetcid'],$resistsoldiers,$resistdefence);
									//$fieldrange=1999;
									//提取出征战术
									$atactics=sql_fetch_one("select * from sys_user_tactics where uid=".$row['uid']);
									//攻击方取得加成
									$att_add=getAdd($row[cid],$row[hid]);
									$att_command=array_shift($att_add);
									$att_attc=array_shift($att_add);
									$att_def=array_shift($att_add);
									$att_speed=array_shift($att_add);
									$att_blood=array_shift($att_add);
									$att_plund=array_shift($att_add);	
									$att_command*=100/$acount;
									if($att_command>1) $att_command=1;
									//我方战术应用
									$atactic=($t_type['type']>0)?$atactics['field']:$atactics['plunder'];
									$atacticArray = explode(";",$atactic);
									$numatactics = count($atacticArray);
									for ($i=0;$i<$numatactics;$i++){
										$soldirs=explode(",",$atacticArray[$i]);
										$atype = array_shift($soldirs);
										for($j=0;$j<$numSoldiers;$j++){
											if($atype==$sid[$j]){
												$action = array_shift($soldirs);
												$dtype = array_shift($soldirs);
												switch ($action){
													case 1:{$pos=100;break;}
													case 2:{$pos=50;break;}
													case 3:{$pos=0;break;}
												}
												$pos=$fieldrange-$pos;
												$attackpos.=$atype.",".$pos.",";
												sql_query("replace into		mem_battle_tactics(battleid,attack,stype,action,target,action2,target2) values('$row[id]','1','$atype','$action','$dtype','$action','$dtype')");
												$soldierinfo=sql_fetch_one("select * from cfg_soldier where sid=".$sid[$j]);
												//射程加成
												$shoot_add=getshoot($sid[$j],$row[cid]);
												$att=floor($soldierinfo['ap']*(1+$att_command*$att_attc/100));
												$def=floor($soldierinfo['dp']*(1+$att_command*$att_def/100));
												$blood=floor($soldierinfo['hp']*(1+$att_command*$att_blood/100));
												$speed=floor($att_speed+$soldierinfo['speed']);
												sql_query("replace into mem_battle_round (battleid,attack,type,sid,count,speed,command,attc,def,hp,plund,pos,shoot) values ('$row[id]','1','$sid[$j]','$sid[$j]','$cnt[$j]','$speed','$att_command','$att','$def','$blood','$att_plund','$pos','$shoot_add')");
												//file_put_contents("log.txt",$j."i\r\n",FILE_APPEND);
											}
										}
									}
									$attackpos=$numSoldiers.",".$attackpos;
									//回合开始
									$nexttime=$nowtime+5;
									//是黄巾军
									if($d_sid[0]>17){
										$resistuid=894;
										sql_query("update sys_battle set resistuid=$resistuid where id=".$row['id']);
									}
									if($resistuid>1000) sql_query("replace into sys_troops(uid,cid,hid,targetcid,task,state,starttime,pathtime,endtime,battleid) values ('$resistuid','$row[targetcid]','$resisthid','$row[targetcid]','$d_task','3','row[endtime]','0','0','$row[id]')");
									//生成战斗部队
									//sql_query("replace into bak_troops(uid,cid,hid,targetcid,task,state,starttime,pathtime,noback,soldiers,resource,battleid,people,fooduse,lastrun,lastlock,lastTempt,lastacc) values('$row[uid]','$row[cid]','$row[hid]','$row[targetcid]','$row[task]','$row[state]','$row[starttime]','$row[pathtime]','$row[noback]','$row[soldiers]','$row[resource]','$row[id]','$row[people]','$row[fooduse]','$row[lastrun]','$row[lastlock]','$row[lastTempt]','$row[lastacc]')");
									//sql_query("replace into bak_troops(uid,cid,hid,targetcid,task,state,starttime,soldiers,battleid) values('$resistuid','$row[targetcid]','$resisthid','$row[targetcid]','$row[task]','$row[state]','$row[starttime]','$resistsoldiers','$row[id]')");
									$a_soid_id=sql_fetch_one_cell("select id from bak_troops where battleid=$row[id] and uid=$row[uid]");
									$d_soid_id=sql_fetch_one_cell("select id from bak_troops where battleid=$row[id] and uid=$resistuid");
									//sql_query("UPDATE sys_city_hero SET state='3' WHERE hid=".$row['hid']);
									//sql_query("UPDATE sys_city_hero SET state='3' WHERE hid=".$resisthid);
									$resistcid=($t_type['ownercid']>0)?$t_type['ownercid']:$row['targetcid'];
									sql_query("replace into mem_battle(id,type,nexttime,round,attackcid,attackhid,attacksoldiers,attackpos,resistcid,resisthid,resistsoldiers,resistpos,resistdefence,wallhp,walllevel,fieldrange,state) values('$row[id]','0','$nexttime','1','$row[cid]','$row[hid]','$row[soldiers]','$attackpos','$resistcid','$resisthid','$resistsoldiers','$resistpos','$resistdefence','$wallhp','$walllevel','$fieldrange','$state')");
									//$dtactics=sql_fetch_one("select field from sys_user_tactics where uid=".$resistuid['uid']);
								}else{//吓跑了
									$content=sprintf($GLOBALS['report']['success'],getCityNamePosition($row['targetcid']),$GLOBALS['report']['type'][$row['task']],$GLOBALS['report']['type'][$row['task']]);
									$content.=$GLOBALS['battle']['none'];
									if($row['task']==4){
										$content.=sprintf($GLOBALS['report']['backend'],getCityNamePosition($row[targetcid]));
										sql_query("update sys_troops set state=4,battleid=0,battlefieldid=0,startcid=0 where id=".$row['id']);
									}
									else{
										$content.=sprintf($GLOBALS['report']['end'],getCityNamePosition($row[cid]));
										sql_query("update sys_troops set state=1,battleid=0,battlefieldid=0,startcid=0 where id=".$row['id']);
									}
									sql_query("UPDATE sys_city_hero SET state='2' WHERE hid=".$row['hid']);
									sql_query("update sys_battle set state=1,result=0 where id=$row[id]");
									$title=getReportTitle($row['task'],3);
									sendReport($row['uid'],0,$title,$row['cid'],$row['targetcid'],$content);
								}
							}
						break;}
					}
				}
				refreshFoodArmyUse($row[cid]);
				$targetwid=cid2wid($row[targetcid]);
				$isfield=sql_fetch_one_cell("select ownercid from mem_world where wid=$targetwid");
				if($isfield<>0){
					refreshFoodArmyUse($isfield);
				}
				//file_put_contents("log.txt","第".$sid."个，时间为：".$time."等级为：".$level."\r\n",FILE_APPEND);
			}
		}
	}
 }


?>

